package cn.gemiman.daoyi.module.iot.open.jhl;

import cn.gemiman.daoyi.module.iot.open.jhl.vo.*;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import static cn.gemiman.daoyi.module.iot.open.jhl.SignUtils.createSign;

@Slf4j
@Service
public class InnoprojhlOpenApiServiceImpl implements InnoprojhlOpenApiService {
    @Value("${openapi.elderlycase.app-id}")
    private String appId;
    @Value("${openapi.elderlycase.app-secret}")
    private String appSecret;
    @Value("${openapi.elderlycase.base-url}")
    private String baseUrl;

    @Override
    public JSONObject createDevice(DeviceCreateReqVO reqVO) {
        reqVO.setAppId(appId);
        reqVO.setTimestamp(System.currentTimeMillis());
        reqVO.setSign(createSign(reqVO, appSecret));
        String responseText = HttpUtil.post(StrUtil.format("{}/device/create", baseUrl), JSONUtil.toJsonStr(reqVO));
        log.info("{}{}", reqVO, responseText);
        return JSONUtil.parseObj(responseText);
    }

    @Override
    public JSONObject queryPlace(PlaceQueryReqVO reqVO) {
        reqVO.setAppId(appId);
        reqVO.setTimestamp(System.currentTimeMillis());
        reqVO.setSign(createSign(reqVO, appSecret));
        String responseText = HttpUtil.post(StrUtil.format("{}/place/query", baseUrl), JSONUtil.toJsonStr(reqVO));
        log.info("{}{}", reqVO, responseText);
        return JSONUtil.parseObj(responseText);
    }


    @Override
    public JSONObject queryDevice(DeviceQueryReqVO reqVO) {
        reqVO.setAppId(appId);
        reqVO.setTimestamp(System.currentTimeMillis());
        reqVO.setSign(createSign(reqVO, appSecret));
        String responseText = HttpUtil.post(StrUtil.format("{}/device/query", baseUrl), JSONUtil.toJsonStr(reqVO));
        log.info("{}{}", reqVO, responseText);
        return JSONUtil.parseObj(responseText);
    }

    @Override
    public JSONObject querySubDevices(QuerySubDevicesReqVO reqVO) {
        reqVO.setAppId(appId);
        reqVO.setTimestamp(System.currentTimeMillis());
        reqVO.setSign(createSign(reqVO, appSecret));
        String responseText = HttpUtil.post(StrUtil.format("{}/device/querySubDevices", baseUrl), JSONUtil.toJsonStr(reqVO));
        log.info("{}{}", reqVO, responseText);
        return JSONUtil.parseObj(responseText);
    }

    @Override
    public JSONObject queryOneDevice(DeviceQueryOneReqVO reqVO) {
        reqVO.setAppId(appId);
        reqVO.setTimestamp(System.currentTimeMillis());
        reqVO.setSign(createSign(reqVO, appSecret));
        String responseText = HttpUtil.post(StrUtil.format("{}/device/queryOne", baseUrl), JSONUtil.toJsonStr(reqVO));
        log.info("{}{}", reqVO, responseText);
        return JSONUtil.parseObj(responseText);
    }

    @Override
    public JSONObject deleteDevice(DeviceDeleteReqVO reqVO) {
        reqVO.setAppId(appId);
        reqVO.setTimestamp(System.currentTimeMillis());
        reqVO.setSign(createSign(reqVO, appSecret));
        String responseText = HttpUtil.post(StrUtil.format("{}/device/delete", baseUrl), JSONUtil.toJsonStr(reqVO));
        log.info("{}{}", reqVO, responseText);
        return JSONUtil.parseObj(responseText);
    }

    @Override
    public JSONObject currentData(DeviceCurrentDataMsgReqVO reqVO) {
        reqVO.setAppId(appId);
        reqVO.setTimestamp(System.currentTimeMillis());
        reqVO.setSign(createSign(reqVO, appSecret));
        String responseText = HttpUtil.post(StrUtil.format("{}/device/currentData", baseUrl), JSONUtil.toJsonStr(reqVO));
        log.info("{}{}", reqVO, responseText);
        return JSONUtil.parseObj(responseText);
    }

    @Override
    public JSONObject glorySend(DeviceGlorySendMsgReqVO reqVO) {
        reqVO.setAppId(appId);
        reqVO.setTimestamp(System.currentTimeMillis());
        reqVO.setSign(createSign(reqVO, appSecret));
        String responseText = HttpUtil.post(StrUtil.format("{}/device/glorySend", baseUrl), JSONUtil.toJsonStr(reqVO));
        log.info("{}{}", reqVO, responseText);
        return JSONUtil.parseObj(responseText);
    }

    @Override
    public JSONObject setContacts(DeviceSetContactsMsgReqVO reqVO) {
        reqVO.setAppId(appId);
        reqVO.setTimestamp(System.currentTimeMillis());
        reqVO.setSign(createSign(reqVO, appSecret));
        String responseText = HttpUtil.post(StrUtil.format("{}/device/setContacts", baseUrl), JSONUtil.toJsonStr(reqVO));
        log.info("{}{}", reqVO, responseText);
        return JSONUtil.parseObj(responseText);
    }

    @Override
    public JSONObject voiceBroadcast(DeviceVoiceBroadcastMsgReqVO reqVO) {
        reqVO.setAppId(appId);
        reqVO.setTimestamp(System.currentTimeMillis());
        reqVO.setSign(createSign(reqVO, appSecret));
        String responseText = HttpUtil.post(StrUtil.format("{}/device/voiceBroadcast", baseUrl), JSONUtil.toJsonStr(reqVO));
        log.info("{}{}", reqVO, responseText);
        return JSONUtil.parseObj(responseText);
    }

    @Override
    public JSONObject setButtonText(DeviceSetButtonTextMsgReqVO reqVO) {
        reqVO.setAppId(appId);
        reqVO.setTimestamp(System.currentTimeMillis());
        reqVO.setSign(createSign(reqVO, appSecret));
        String responseText = HttpUtil.post(StrUtil.format("{}/device/setButtonText", baseUrl), JSONUtil.toJsonStr(reqVO));
        log.info("{}{}", reqVO, responseText);
        return JSONUtil.parseObj(responseText);
    }

    @Override
    public JSONObject setStatePushSwitch(DeviceSetStatePushSwitchMsgReqVO reqVO) {
        reqVO.setAppId(appId);
        reqVO.setTimestamp(System.currentTimeMillis());
        reqVO.setSign(createSign(reqVO, appSecret));
        String responseText = HttpUtil.post(StrUtil.format("{}/device/setStatePushSwitch", baseUrl), JSONUtil.toJsonStr(reqVO));
        log.info("{}{}", reqVO, responseText);
        return JSONUtil.parseObj(responseText);
    }
}
